<?php
/**
 * missing
 * 
 * @package   
 * @author My Application Center
 * @created Anthony Aldridge
 * @version 2012
 * @access public
 */
class missing
{
    public $state = '';
    public $rand = false;
    public $url = 'http://www.missingkids.com/missingkids/servlet/XmlServlet?act=rss';
    public function debugView()
    {
        $xml = $this->loadXml();
        echo "<pre>";
        print_r($xml);
        echo "</pre>";
    }

    protected function loadXml()
    {
        if ($this->state == '') {
            $xml = simplexml_load_file($this->url);
        } else {
            $state = strtoupper($this->state);
            $xml = simplexml_load_file("http://www.missingkids.com/missingkids/servlet/XmlServlet?act=rss&LanguageCountry=en_US&orgPrefix=NCMC&state=$state");
        }
        return $xml;
    }
    /**
     * missing::return_data()
     * returns randomly selected entry then the variables without any imaging
     * @param mixed $state
     * @return
     */
    public function return_data()
    {
        if ($this->state == '') {
            $xml = simplexml_load_file($this->url);
        } else {
            $state = strtoupper($this->state);
            $xml = simplexml_load_file("http://www.missingkids.com/missingkids/servlet/XmlServlet?act=rss&LanguageCountry=en_US&orgPrefix=NCMC&state=$state");
        }
        $hnmb = $xml->channel->item->count();
        $numb = rand(0, $hnmb - 1);
        $string = $xml->channel->item[$numb]->description;
        $img = $xml->channel->item[$numb]->enclosure['url'];
        $f_c = strpos($string, ",");
        $name = substr($string, 0, $f_c);
        $exp = explode(",", $string, 3);
        $g_a = strpos($string, "Now:");
        $g_b = strpos($string, ", M");
        $age = substr($exp['1'], -2, 2);
        $g_p = strrpos($string, " ");
        $phone = substr($string, $g_p + 3);
        $phone = str_replace(".", "", $phone);
        $grab_date = substr($exp['2'], 1, 19);
        $regex = '/(\d{3})-(\d{3})-(\d{4})/';
        $phone = preg_match($regex, $string);
        $ph_x2 = preg_match_all('/(\d{3})-(\d{3})-(\d{4})/', $string, $matches);
        $phone = $matches[0][0];
        $set['name'] = $name;
        $set['desc'] = $string;
        $set['phone'] = $phone;
        $set['date'] = $grab_date;
        $set['img'] = $img;
        $set['age'] = $age;
        $set['id'] = $numb;
        return $set;
    }
    /**
     * missing::buildimg_random_bystate()
     * Receives input using standard 2 letter state (ex AR = Arkansas) outputs a 200x300 banner
     * @param mixed $state
     * @return
     */
    public function buildimg_random_bystate()
    {
        $dataset = $this->return_data();

        $img_base = imagecreatefrompng("miss_badge.png");
        $img_miss = imagecreatefromjpeg($dataset['img']);
        $img_width = 200;
        if ($img_miss !== false) {
            $red_alert = imagecolorallocate($img_base, 139, 37, 0);
            $text_colour = imagecolorallocate($img_base, 0, 0, 0);
            $line_colour = imagecolorallocate($img_base, 0, 0, 255);
            imagefttext($img_base, 32, 0, 61, 41, $red_alert, "./BLANCH_CONDENSED.ttf", "MISSING");
            $position_center = $this->center_it($dataset['name'], 5, $img_width);
            imagestring($img_base, 5, $position_center, 45, ucwords(strtolower($dataset['name'])), $text_colour);
            imagesetthickness($img_base, 5);
            imageline($img_base, 10, 65, 190, 65, $line_colour);
            $position_center = $this->center_it($dataset['date'], 3, $img_width);
            imagestring($img_base, 3, $position_center, 73, $grab_date, $text_colour);
            $position_center = $this->center_it("Current Age", 48, $img_width);
            imagestring($img_base, 5, $position_center, 185, "Current Age", $red_alert);
            imagefttext($img_base, 48, 0, 80, 250, $red_alert, "./BLANCH_CONDENSED.ttf", $dataset['age']);
            $position_center = $this->center_it("Seen this person?", 48, $img_width);
            imagestring($img_base, 5, $position_center, 255, "Seen this person?", $text_colour);
            $position_center = $this->center_it($dataset['phone'], 48, $img_width);
            imagestring($img_base, 5, $position_center, 270, $dataset['phone'], $text_colour);
            imagestring($img_base, 1, 175, 285, $dataset['id'], $text_colour);
            $marge_right = 70;
            $marge_bottom = 120;
            $sx = imagesx($img_miss);
            $sy = imagesy($img_miss);
            imagecopy($img_base, $img_miss, imagesx($img_base) - $sx - $marge_right, imagesy
                ($img_base) - $sy - $marge_bottom, 0, 0, 64, 80);
            header("Content-type: image/jpeg");
            imagejpeg($img_base, null, 85);
            imagecolordeallocate($line_color);
            imagecolordeallocate($text_color);
            imagecolordeallocate($background);
        } else {
            die("Error retrieving the remote missing persons image.");
        }
        imagedestroy($img_base);
        imagedestroy($img_miss);
    }
    /**
     * missing::buildimg_random_bystate_468x60()
     * Receives input using standard 2 letter state (ex AR = Arkansas) outputs a 468x60 banner
     * @param mixed $state
     * @return
     */
    public function buildimg_random_bystate_468x60()
    {
        $dataset = $this->return_data();
        $img_base = imagecreatefrompng("./miss_badge_468x60.png");
        $img_miss = @imagecreatefromjpeg($dataset['img']);
        $sx = imagesx($img_miss);
        $sy = imagesy($img_miss);
        $n_x = 38;
        $n_y = 48;
        $image_p = imagecreatetruecolor($n_x, $n_y);
        $img2 = imagecopyresampled($img_miss, $img_miss, 0, 0, 0, 0, $n_x, $n_y, $sx, $sy);
        $dpb = "Data Provided by: National Center for Missing & Exploited Children";
        if ($img_miss !== false) {
            $red_alert = imagecolorallocate($img_base, 139, 37, 0);
            $text_colour = imagecolorallocate($img_base, 0, 0, 0);
            $line_colour = imagecolorallocate($img_base, 0, 0, 255);
            imagestring($img_base, 4, 145, 3, ucwords(strtolower($dataset['name'])), $text_colour);
            imagestring($img_base, 3, 145, 18, $dataset['date'], $text_colour);
            imagestring($img_base, 3, 145, 18 + 15, "Age: " . $dataset['age'], $text_colour);
            imagestring($img_base, 5, 300, 15, "Seen this person?", $text_colour);
            imagestring($img_base, 3, 330, 30, $dataset['phone'], $text_colour);
            imagestring($img_base, 1, 450, 50, $dataset['id'], $text_colour);
            $center = $this->center_it($dpb, 1, 468);
            imagestring($img_base, 1, $center, 50, $dpb, $text_colour);
            imagecopy($img_base, $img_miss, 105, 3, 0, 0, $n_x, $n_y);
            header("Content-type: image/jpeg");
            imagejpeg($img_base, null, 85);
            imagecolordeallocate($line_color);
            imagecolordeallocate($text_color);
            imagecolordeallocate($background);
        } else {
            die("Error retrieving the remote missing persons image.");
        }
        imagedestroy($img_base);
        imagedestroy($img_miss);
    }
    /**
     * missing::buildimg_random()
     * Receives no input outputs a 200x300 banner
     * @return
     */
    public function buildimg_random()
    {
        $dataset = $this->return_data();
        $img_base = imagecreatefrompng("./miss_badge.png");
        $img_miss = @imagecreatefromjpeg($dataset['img']);
        if ($img_miss !== false) {
            $background = imagecolorallocate($img_base, 211, 211, 211);
            $red_alert = imagecolorallocate($img_base, 139, 37, 0);
            $text_colour = imagecolorallocate($img_base, 0, 0, 0);
            $line_colour = imagecolorallocate($img_base, 128, 255, 0);
            $strtotime = strtotime($pubDate);
            $date = date("m-d-Y h:i a", $strtotime);
            imagefttext($img_base, 32, 0, 61, 41, $red_alert, "./BLANCH_CONDENSED.ttf","MISSING");
            $font_width = ImageFontWidth('5');
            $text_width = $font_width * strlen($dataset['name']);
            $position_center = ceil((200 - $text_width) / 2);
            imagestring($img_base, 5, $position_center, 45, ucwords(strtolower($dataset['name'])), $text_colour);
            imagesetthickness($img_base, 5);
            imageline($img_base, 10, 65, 190, 65, $line_colour);
            imagestring($img_base, 3, 37, 73, $dataset['date'], $text_colour);
            imagestring($img_base, 5, 85, 185, "Age", $red_alert);
            imagefttext($img_base, 48, 0, 81, 250, $red_alert, "./BLANCH_CONDENSED.ttf", $dataset['age']);
            imagestring($img_base, 5, 23, 255, "Seen this person?", $text_colour);
            imagestring($img_base, 5, 43, 270, $dataset['phone'], $text_colour);
            imagestring($img_base, 1, 185, 290, $dataset['id'], $text_colour);
            $marge_right = 70;
            $marge_bottom = 120;
            $sx = imagesx($img_miss);
            $sy = imagesy($img_miss);
            imagecopy($img_base, $img_miss, imagesx($img_base) - $sx - $marge_right, imagesy
                ($img_base) - $sy - $marge_bottom, 0, 0, imagesx($img_miss), imagesy($img_miss));
            header("Content-type: image/jpeg");
            imagejpeg($img_base, null, 85);
            imagecolordeallocate($line_color);
            imagecolordeallocate($text_color);
            imagecolordeallocate($background);
        } else {
            die("Error retrieving the remote missing persons image.");
        }
        imagedestroy($img_base);
        imagedestroy($img_miss);
    }
    /**
     * missing::buildimg_random_bystate_728x90()
     * Receives input using standard 2 letter state (ex AR = Arkansas) outputs a 728x90 banner
     * @param mixed $state
     * @return
     */
    public function buildimg_random_bystate_728x90()
    {
        $dataset = $this->return_data();
        $img_base = imagecreatefrompng("./miss_badge_728x90.png");
        $img_miss = @imagecreatefromjpeg($dataset['img']);
        $sx = imagesx($img_miss);
        $sy = imagesy($img_miss);
        $img_width = 768;
        $dpb = "Data Provided by: National Center for Missing & Exploited Children";
        if ($img_miss !== false) {
            $red_alert = imagecolorallocate($img_base, 139, 37, 0);
            $text_colour = imagecolorallocate($img_base, 0, 0, 0);
            $line_colour = imagecolorallocate($img_base, 0, 0, 255);
            imagestring($img_base, 5, 235, 15, ucwords(strtolower($dataset['name'])), $text_colour);
            imagestring($img_base, 4, 235, 30, $dataset['date'], $text_colour);
            imagestring($img_base, 4, 235, 45, "Age: " . $dataset['age'], $text_colour);
            imagestring($img_base, 5, 500, 15, "Seen this person?", $text_colour);
            imagestring($img_base, 4, 525, 30, $dataset['phone'], $text_colour);
            imagestring($img_base, 1, 700, 75, $dataset['id'], $text_colour);
            $center = $this->center_it($dpb, 1, $img_width) + 10;
            imagestring($img_base, 1, $center, 78, $dpb, $text_colour);
            imagecopy($img_base, $img_miss, 158, 3, 0, 0, $sx, $sy);
            header("Content-type: image/jpeg");
            imagejpeg($img_base, null, 85);
            imagecolordeallocate($line_color);
            imagecolordeallocate($text_color);
            imagecolordeallocate($background);
        } else {
            die("Error retrieving the remote missing persons image.");
        }
        imagedestroy($img_base);
        imagedestroy($img_miss);
    }
    /**
     * missing::center_it()
     * Centers the text based on image size
     * @param mixed $text
     * @param mixed $font_width
     * @param mixed $img_width
     * @return
     */
    private function center_it($text, $font_width, $img_width)
    {
        $font_width = ImageFontWidth($font_width);
        $text_width = $font_width * strlen($text);
        $position_center = ceil(($img_width - $text_width) / 2);
        return $position_center;
    }
}
?>